import {Component, OnInit, ViewChild} from '@angular/core';
import {PageHelper} from '../../../../utils/page-helper-model';
import {UserInfoModel} from '../../../../models/user-info.model';
import {BaseService} from '../../../../shared/service/base.service';
import {RoleService} from '../../../../service/role.service';
import {Consts} from '../../../../config/consts';
import {UserService} from '../../../../service/user.service';
import {UserStatusEnum} from '../../../../enums/user-status.enum';
import {Globals} from '../../../../globals';

@Component({
  selector: 'app-staff-list',
  templateUrl: './staff-list.component.html',
  styleUrls: ['./staff-list.component.less']
})
export class StaffListComponent implements OnInit {

  isMobile = Globals.isMobile();
  isEditVisible = false;
  pageHelper: PageHelper = new PageHelper;
  userList;
  dimissionUserList: any;
  user = {};
  //调用子组件的东西
  @ViewChild('edit') edit;

  roleId;

  userInfo: UserInfoModel;

  status: UserStatusEnum;
  code: number;
  pwdVisible: any;
  userStatus = {
    DISLODGE: UserStatusEnum.DISLODGE
  };
  dimissionIsVisible = false;

  constructor(
    private httpService: BaseService,
    private userService: UserService,
    private roleService: RoleService) {
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
  }

  ngOnInit() {
    this.findStaffList();
  }


  //查找员工列表
  findStaffList(code = 0) {
    this.code = code;
    // this.userInfo.pageHelper = this.pageHelper;
    // this.userInfo.numPages = this.pageHelper.numPages;
    let body = {
      'pageNum': this.pageHelper.currentPage,
      'keyword': this.pageHelper.keyword,
      'roleId': code
    };
    this.userService.findByNameAndMobile(this.pageHelper, body, res => {
      this.userList = res.list;
      this.pageHelper = res.pageHelper;
    });
  }

  /**
   * 把员工设为离职状态
   * @param data
   * @param status
   */
  delete(data, status) {
    this.userService.layer.modalDeleteConfirm('确定要将该员工离职吗？', confirm => {
      if (confirm) {
        let body = {
          'id': data.id,
          'username': data.username,
          'status': status,
          'agencyId': data.agencyId,
        };
        this.userService.updateUserStatusById(body, data => {
          setTimeout(() => {
            this.findStaffList(this.code);
          }, 500);
        });
        // this.userService.deleteUser(data.id,res=>{
        //   this.userService.getResponse(res)
        // })
      }
    });
  }

  /**
   * 员工编辑更新Modal弹窗(弹出)
   * @param user
   */
  update(user) {
    this.isEditVisible = true;
    this.edit.load(user);
  }


  setEditVisible(body) {
    this.isEditVisible = body.isVisible;
    if (!body.cancel) {
      this.findStaffList();
    }
  }

  nzPageIndexChange(event: number) {
    this.pageHelper.numPages = event;
    this.findStaffList(this.code);
  }

  updatePwd(data) {
    this.userService.layer.modalConfirm('重置后密码为手机号(手机号为空则123456)', confirm => {
      if (confirm) {
        let user = {id: data.id};
        this.userService.resetPwdById(user);
      }
    }, '确定要重置密码吗?');
  }

  setPwdVisible($event: any) {
    this.pwdVisible = $event;
  }

  search() {
    this.findStaffList();
  }

  /**
   * 添加员工弹窗
   */
  addUser() {
    this.isEditVisible = true;
    this.edit.load(null);
  }

  /**
   * 弹出离职员工的modal框
   */
  dimissionUser() {
    this.userLoad();
    this.dimissionLoad();
    this.dimissionIsVisible = true;
  }

  /**
   * 取消离职员工列表Modal弹窗的方法
   * @param flag
   */
  dimissionFlag(flag) {
    this.dimissionIsVisible = flag;
  }

  /**
   * 获取当前登录用户的信息
   */
  userLoad() {
    this.userService.userInfo(res => {
      this.userInfo = res.data;
    });
  }

  /**
   * 离职员工列表加载
   */
  dimissionLoad() {
    let body = {
      'pageNum': this.pageHelper.currentPage,
      // "keyword": this.pageHelper.keyword
    };
    this.userService.findDimissionUser(this.pageHelper, body, res => {
      this.dimissionUserList = res.list;
      this.pageHelper = res.pageHelper;
    });
  }

}
